﻿using System;
using Lazy.Captcha.Core.Storage;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;

namespace Lazy.Captcha.Core.Storage
{
    public class DefaultStorage(IOptionsMonitor<CaptchaOptions> options, IDistributedCache cache) : IStorage
    {
        private string WrapKey(string key)
        {
            return $"{options.CurrentValue.StorageKeyPrefix}{key}";
        }

        public string Get(string key)
        {
            return cache.GetString(WrapKey(key));
        }

        public void Remove(string key)
        {
            cache.Remove(WrapKey(key));
        }

        public void Set(string key, string value, DateTimeOffset absoluteExpiration)
        {
            cache.SetString(WrapKey(key), value, new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = absoluteExpiration
            });
        }
    }
}